//package com.example.communitypro.interceptor;
//
//import com.auth0.jwt.exceptions.AlgorithmMismatchException;
//import com.auth0.jwt.exceptions.SignatureVerificationException;
//import com.auth0.jwt.exceptions.TokenExpiredException;
//import com.example.communitypro.mapper.UserInformationMapper;
//import com.example.communitypro.pojo.UserInformation;
//import com.example.communitypro.service.UserInformationService;
//import com.example.communitypro.service.impl.UserInformationServiceImpl;
//import com.example.communitypro.util.GetAllUrl;
//import com.example.communitypro.util.JWTUtils;
//import com.example.communitypro.util.SpringUtil;
//import com.example.communitypro.util.result.Result;
//import com.fasterxml.jackson.databind.ObjectMapper;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationContext;
//import org.springframework.web.method.HandlerMethod;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//public class JWTInterceptor implements HandlerInterceptor {
//
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
//        Map<String,Object> map = new HashMap<>();
//        String token = request.getHeader("token");//获取请求头中的令牌
//        if(!(handler instanceof HandlerMethod)){
//            return true;
//        }
////        System.out.println("token："+token);
//        if (GetAllUrl.isSuccess(request)){
//            return true;
//        }
//        /*if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
//            String[] split = request.getRequestURI().split("/");
//
//            map.put("url",split[1]);
//        }*/
//
//        try{
//            JWTUtils.verify(token);
//            String state = JWTUtils.getUserlevel(request);
//            Integer state1 = new Integer(state);
//            if (state != null){
//
//                String sp = request.getServletPath();
//
//                /**
//                 * lsl
//                 * 获取所有url
//                 */
//                /*GetAllUrl getAllUrl = new GetAllUrl();
//                List<String> allUrl = getAllUrl.getAllUrl();*/
//
//
//                String[] paths0 = new String[]{"/login","/post/find","/post/like","/file/uploadPostPhoto","/file/deletePostPhoto",
//                        "/post/recommend", "/post/addPost","/communityInformation/listCommunityAll","/post/queryPost","/userDetails/queryUserDetailsByUser",
//                        "/userInformation/queryUserInformation","/userLikes/UserLikes","/userCollection/UserCollection",
//                        "/comment/addComment","/comment/queryPostComment","/comment/queryHotComment","/report/addUserReport","/swagger-ui.html"};//普通用户路径
////                String[] paths1 = new String[allUrl.size()];//管理员路径
//                String[] paths1 = new String[]{"/admin"};//管理员路径
//                /**
//                 * lsl
//                 */
//               /* for (int i = 0; i < allUrl.size(); i++) {
//                    paths1[i] = allUrl.get(i);
//                }*/
//
//                if(state1 == 0){
//                    for (String path : paths0) {
//                        if (path.equals(sp)) {
//                            // 权限符合
//                            return true;
//                        }
//                    }
//                }
//                else if (state1 == 1){
//                    /*for (String path : paths1) {
//                        if (path.equals(sp)) {
//                            // 权限符合
//                            return true;
//                        }
//                    }*/
//                    return true;
//                }
//                else{
//                    System.out.println("我嘞哇拦截你的嘛");
//                    //request.getRequestDispatcher("login.jsp").forward(request, response);//权限不符合
//                    return false;
//                }
//            }
//            else
//                return false;
//        }catch (SignatureVerificationException e){
////            e.printStackTrace();
//            map.put("msg","无效签名！");
//        }catch (TokenExpiredException e){
////            e.printStackTrace();
//            map.put("msg","token过期！");
//        }catch (AlgorithmMismatchException e){
////            e.printStackTrace();
//            map.put("msg","token算法不一致！");
//        } catch (Exception e){
////            e.printStackTrace();
//            map.put("msg","token无效！");
//        }
//        map.put("state",false);//设置状态
//        //将map  转换json jackson
//        String json = new ObjectMapper().writeValueAsString(map);
//        response.setContentType("application/json;charset=UTF-8");
////        response.sendRedirect("/community/toLoginIn");
////        request.getRequestDispatcher("/community/toLoginIn").forward(request,response);
//        response.getWriter().println(json);
//        return false;
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//    }
//}
